library(tidyverse)
library(janitor)
library(lubridate)
library(tsibble)
# library(tsibbledata)
library(feasts) # Possibly get dev version (gg_season issue): remotes::install_github("tidyverts/feasts")
library(forecast)
library(paletteer)
We’ll explore, then forecast, US energy consumption and production by renewables source. Get the data from renewables_cons_prod.csv:
us_renew <- read_csv("renewables_cons_prod.csv") %>%
clean_names()
Explore the data frame:
# View(us_renew)
# names(us_renew)
# unique(us_renew$description)
We’ll focus on consumption data.
renew_clean <- us_renew %>%
mutate(description = str_to_lower(description)) %>%
filter(str_detect(description, pattern = "consumption")) %>%
filter(!str_detect(description, pattern = "total"))
yyyymm column to tsibble with lubridaterenew_date <- renew_clean %>%
mutate(month = lubridate::parse_date_time(yyyymm, "ym")) %>%
mutate(month = yearmonth(month)) %>% #coerce to `yearmonth` format
mutate(value = as.numeric(value)) %>%
drop_na(month, value)
Make, then save, the base line plot as renew_gg:
renew_gg <- ggplot(data = renew_date, aes(x = month, y = value, group = description)) +
geom_line(aes(color = description)) +
theme_minimal() +
scale_y_continuous(limits = c(0, 350))
Now try updating your color palette using options from paletteer. Use View(palettes_d_names) to see all of the discrete scale options. We’ll want a palette with at least 7 colors (length >= 7). Find a name of a palette that you like, then update your graph by adding scale_color_paletteer_d("package::palette"). Like, if I want to use the calecopal::figmtn palette, I’d add:
renew_gg + scale_color_paletteer_d("calecopal::figmtn")
Try some out!
renew_gg +
scale_color_paletteer_d("calecopal::figmtn")
Have some fun trying out different color palettes.
renew_ts <- as_tsibble(renew_date, key = description, index = month)
renew_ts %>% autoplot(value)
renew_ts %>% gg_subseries(value)
renew_ts %>% gg_season(value)
hydro_ts <- renew_ts %>%
filter(description == "hydroelectric power consumption")
# Explore:
hydro_ts %>% autoplot(value)
hydro_ts %>% gg_subseries(value)
hydro_ts %>% gg_season(value)
First, let’s check the decomposition (STL):
# Find STL decomposition
dcmp <- hydro_ts %>%
model(STL(value ~ season(window = Inf)))
# View the components
# components(dcmp)
# Visualize the decomposed components
components(dcmp) %>% autoplot() +
theme_minimal()